<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="利润单号" prop="profitBillNumber">
        <el-input
          v-model="queryParams.profitBillNumber"
          placeholder="请输入利润单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="利润月份" prop="profitBillDate">
        <el-date-picker clearable
          v-model="queryParams.profitBillDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择利润月份">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结算客户" prop="statisticFieldName">
        <el-select v-model="queryParams.statisticFieldName" placeholder="请选择结算客户" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="运输重量" prop="freightWeightSum">
        <el-input
          v-model="queryParams.freightWeightSum"
          placeholder="请输入运输重量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运费金额" prop="freightAmountSum">
        <el-input
          v-model="queryParams.freightAmountSum"
          placeholder="请输入运费金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--el-form-item label="工资合计" prop="salaryAmountSum">
        <el-input
          v-model="queryParams.salaryAmountSum"
          placeholder="请输入工资合计"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="现金补气" prop="cashSupplementGasSum">
        <el-input
          v-model="queryParams.cashSupplementGasSum"
          placeholder="请输入现金补气"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="加气费" prop="addGasFeeSum">
        <el-input
          v-model="queryParams.addGasFeeSum"
          placeholder="请输入加气费"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="路费(现金)" prop="tollFeeCashSum">
        <el-input
          v-model="queryParams.tollFeeCashSum"
          placeholder="请输入路费(现金)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="路费(ETC)" prop="tollFeeEtcSum">
        <el-input
          v-model="queryParams.tollFeeEtcSum"
          placeholder="请输入路费(ETC)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="罚款" prop="forfeitSum">
        <el-input
          v-model="queryParams.forfeitSum"
          placeholder="请输入罚款"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="信息费" prop="informationFeeSum">
        <el-input
          v-model="queryParams.informationFeeSum"
          placeholder="请输入信息费"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修车费" prop="repairTruckFeeSum">
        <el-input
          v-model="queryParams.repairTruckFeeSum"
          placeholder="请输入修车费"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务站修车" prop="serviceStationRepairSum">
        <el-input
          v-model="queryParams.serviceStationRepairSum"
          placeholder="请输入服务站修车"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="保险费" prop="insurancePremiumSum">
        <el-input
          v-model="queryParams.insurancePremiumSum"
          placeholder="请输入保险费"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="轮胎费" prop="tireFeeSum">
        <el-input
          v-model="queryParams.tireFeeSum"
          placeholder="请输入轮胎费"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="其他" prop="otherFeeSum">
        <el-input
          v-model="queryParams.otherFeeSum"
          placeholder="请输入其他"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支出总计" prop="expenseTotalSum">
        <el-input
          v-model="queryParams.expenseTotalSum"
          placeholder="请输入支出总计"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="利润合计" prop="profitSum">
        <el-input
          v-model="queryParams.profitSum"
          placeholder="请输入利润合计"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统计字段" prop="statisticField">
        <el-select v-model="queryParams.statisticField" placeholder="请选择统计字段" clearable>
          <el-option
            v-for="dict in dict.type.statistic_field"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="统计粒度" prop="statisticGranularity">
        <el-select v-model="queryParams.statisticGranularity" placeholder="请选择统计粒度" clearable>
          <el-option
            v-for="dict in dict.type.statistic_granularity"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="园区代码" prop="parkCode">
        <el-input
          v-model="queryParams.parkCode"
          placeholder="请输入园区代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item-->
      <el-form-item label="创建者" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 205px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['finance:profit:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['finance:profit:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['finance:profit:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['finance:profit:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" @computeTable="computeTableHeight"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="profitList" @selection-change="handleSelectionChange" border :height="tableHeight" :row-style="{height:'35px'}" :cell-style="{padding:'0px'}">
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column type="index" prop="" width="55" label="序号" align="center" >
        <template slot-scope="scope">
          <span>{{ scope.$index+1 +(queryParams.pageNum-1)*queryParams.pageSize }}</span>
        </template>
      </el-table-column>
      <!--el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100%">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['finance:profit:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['finance:profit:remove']"
          >删除</el-button>
        </template>
      </el-table-column-->
      <!--el-table-column label="账单ID" align="center" prop="profitBillId" /-->
      <el-table-column label="利润单号" align="center" prop="profitBillNumber" width="160" :show-overflow-tooltip="true" />
      <el-table-column label="利润月份" align="center" prop="profitBillDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.profitBillDate, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算客户" align="center" prop="statisticFieldName" width="300" :show-overflow-tooltip="true" />
      <el-table-column label="运输重量" align="center" prop="freightWeightSum" />
      <el-table-column label="运费金额" align="center" prop="freightAmountSum" />
      <el-table-column label="工资合计" align="center" prop="salaryAmountSum" />
      <el-table-column label="现金补气" align="center" prop="cashSupplementGasSum" />
      <el-table-column label="加气费" align="center" prop="addGasFeeSum" />
      <el-table-column label="路费(现金)" align="center" prop="tollFeeCashSum"  width="100" />
      <el-table-column label="路费(ETC)" align="center" prop="tollFeeEtcSum" width="100" />
      <el-table-column label="罚款" align="center" prop="forfeitSum" />
      <el-table-column label="信息费" align="center" prop="informationFeeSum" />
      <el-table-column label="修车费" align="center" prop="repairTruckFeeSum" />
      <el-table-column label="服务站修车" align="center" prop="serviceStationRepairSum"  width="100" />
      <el-table-column label="保险费" align="center" prop="insurancePremiumSum" />
      <el-table-column label="轮胎费" align="center" prop="tireFeeSum" />
      <el-table-column label="其他" align="center" prop="otherFeeSum" />
      <el-table-column label="支出总计" align="center" prop="expenseTotalSum" />
      <el-table-column label="利润合计" align="center" prop="profitSum" />
      <el-table-column label="备注" align="center" prop="remark" />
      <!--el-table-column label="统计字段" align="center" prop="statisticField" />
      <el-table-column label="统计粒度" align="center" prop="statisticGranularity" /-->
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改利润账单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="利润单号" prop="profitBillNumber">
          <el-input v-model="form.profitBillNumber" placeholder="请输入利润单号" />
        </el-form-item>
        <el-form-item label="利润月份" prop="profitBillDate">
          <el-date-picker clearable
            v-model="form.profitBillDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择利润月份"
            style="width: 340px;">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结算客户" prop="statisticFieldName">
          <el-select v-model="form.statisticFieldName" placeholder="请选择结算客户" style="width: 340px;">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="运输重量" prop="freightWeightSum">
          <el-input v-model="form.freightWeightSum" placeholder="请输入运输重量" />
        </el-form-item>
        <el-form-item label="运费金额" prop="freightAmountSum">
          <el-input v-model="form.freightAmountSum" placeholder="请输入运费金额" />
        </el-form-item>
        <el-form-item label="工资合计" prop="salaryAmountSum">
          <el-input v-model="form.salaryAmountSum" placeholder="请输入工资合计" />
        </el-form-item>
        <el-form-item label="现金补气" prop="cashSupplementGasSum">
          <el-input v-model="form.cashSupplementGasSum" placeholder="请输入现金补气" />
        </el-form-item>
        <el-form-item label="加气费" prop="addGasFeeSum">
          <el-input v-model="form.addGasFeeSum" placeholder="请输入加气费" />
        </el-form-item>
        <el-form-item label="路费(现金)" prop="tollFeeCashSum">
          <el-input v-model="form.tollFeeCashSum" placeholder="请输入路费(现金)" />
        </el-form-item>
        <el-form-item label="路费(ETC)" prop="tollFeeEtcSum">
          <el-input v-model="form.tollFeeEtcSum" placeholder="请输入路费(ETC)" />
        </el-form-item>
        <el-form-item label="罚款" prop="forfeitSum">
          <el-input v-model="form.forfeitSum" placeholder="请输入罚款" />
        </el-form-item>
        <el-form-item label="信息费" prop="informationFeeSum">
          <el-input v-model="form.informationFeeSum" placeholder="请输入信息费" />
        </el-form-item>
        <el-form-item label="修车费" prop="repairTruckFeeSum">
          <el-input v-model="form.repairTruckFeeSum" placeholder="请输入修车费" />
        </el-form-item>
        <el-form-item label="服务站修车" prop="serviceStationRepairSum">
          <el-input v-model="form.serviceStationRepairSum" placeholder="请输入服务站修车" />
        </el-form-item>
        <el-form-item label="保险费" prop="insurancePremiumSum">
          <el-input v-model="form.insurancePremiumSum" placeholder="请输入保险费" />
        </el-form-item>
        <el-form-item label="轮胎费" prop="tireFeeSum">
          <el-input v-model="form.tireFeeSum" placeholder="请输入轮胎费" />
        </el-form-item>
        <el-form-item label="其他" prop="otherFeeSum">
          <el-input v-model="form.otherFeeSum" placeholder="请输入其他" />
        </el-form-item>
        <el-form-item label="支出总计" prop="expenseTotalSum">
          <el-input v-model="form.expenseTotalSum" placeholder="请输入支出总计" />
        </el-form-item>
        <el-form-item label="利润合计" prop="profitSum">
          <el-input v-model="form.profitSum" placeholder="请输入利润合计" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="统计字段" prop="statisticField">
          <el-select v-model="form.statisticField" placeholder="请选择统计字段" style="width: 340px;">
            <el-option
              v-for="dict in dict.type.statistic_field"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="统计粒度" prop="statisticGranularity">
          <el-select v-model="form.statisticGranularity" placeholder="请选择统计粒度" style="width: 340px;">
            <el-option
              v-for="dict in dict.type.statistic_granularity"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="园区代码" prop="parkCode">
          <el-input v-model="form.parkCode" placeholder="请输入园区代码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProfit, getProfit, delProfit, addProfit, updateProfit } from "@/api/finance/profit";

export default {
  name: "Profit",
  dicts: ['statistic_granularity', 'statistic_field', 'sys_yes_no'],
  data() {
    return {
      // 定义列表高度
      tableHeight: 430,
      // 定义查询高度
      queryHeight: 200,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 利润账单表格数据
      profitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 园区代码时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        profitBillNumber: null,
        profitBillDate: null,
        statisticFieldName: null,
        freightWeightSum: null,
        freightAmountSum: null,
        salaryAmountSum: null,
        cashSupplementGasSum: null,
        addGasFeeSum: null,
        tollFeeCashSum: null,
        tollFeeEtcSum: null,
        forfeitSum: null,
        informationFeeSum: null,
        repairTruckFeeSum: null,
        serviceStationRepairSum: null,
        insurancePremiumSum: null,
        tireFeeSum: null,
        otherFeeSum: null,
        expenseTotalSum: null,
        profitSum: null,
        remark: null,
        statisticField: null,
        statisticGranularity: null,
        parkCode: null,
        createBy: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        profitBillNumber: [
          { required: true, message: "利润单号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.$nextTick(() =>{
      // 根据浏览器高度设置初始高度 - 按钮行高度28 - 页码行高度28 - （面包屑+TAB行）高度160
      this.queryHeight = this.$refs.queryForm.$el.offsetHeight;
      this.tableHeight = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 28 - 28 - 160;
      // 监听浏览器高度变化，改变表格高度
      window.onresize = () =>{
        this.tableHeight = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 28 - 28 - 160;
      }
    })
  },
  methods: {
    /** 查询利润账单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listProfit(this.queryParams).then(response => {
        this.profitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        profitBillId: null,
        profitBillNumber: null,
        profitBillDate: null,
        statisticFieldName: null,
        freightWeightSum: null,
        freightAmountSum: null,
        salaryAmountSum: null,
        cashSupplementGasSum: null,
        addGasFeeSum: null,
        tollFeeCashSum: null,
        tollFeeEtcSum: null,
        forfeitSum: null,
        informationFeeSum: null,
        repairTruckFeeSum: null,
        serviceStationRepairSum: null,
        insurancePremiumSum: null,
        tireFeeSum: null,
        otherFeeSum: null,
        expenseTotalSum: null,
        profitSum: null,
        remark: null,
        statisticField: null,
        statisticGranularity: null,
        dataRealm: null,
        parkCode: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.profitBillId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加利润账单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const profitBillId = row.profitBillId || this.ids
      getProfit(profitBillId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改利润账单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.profitBillId != null) {
            updateProfit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProfit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const profitBillIds = row.profitBillId || this.ids;
      this.$modal.confirm('是否确认删除利润账单编号为"' + profitBillIds + '"的数据项？').then(function() {
        return delProfit(profitBillIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('finance/profit/export', {
        ...this.queryParams
      }, `profit_${new Date().getTime()}.xlsx`)
    },
    computeTableHeight(){
      if( this.showSearch==true ){
        this.tableHeight = window.innerHeight - this.queryHeight - 28 - 28 - 160;
      } else {
        this.tableHeight = window.innerHeight - 28 - 28 - 160;
      }
    }
  }
};
</script>
